package com.lineage.server.clientpackets;

import com.lineage.echo.ClientExecutor;
import com.lineage.server.Instance.character.CharacterPunishInstance;
import com.lineage.server.Shutdown;
import com.lineage.server.datatables.ItemRestrictionsTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1PetInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1PolyMorph;
import com.lineage.server.serverpackets.S_CloseList;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_DoActionShop;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1PrivateShopBuyList;
import com.lineage.server.templates.L1PrivateShopSellList;
import com.lineage.server.timecontroller.server.ServerRestartTimer;
import com.lineage.server.types.Point;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_Shop extends ClientBasePacket {
  private static final Log _log;
  
  private static final Integer MAX_PRICE = Integer.valueOf(100000000);
  
  static {
    _log = LogFactory.getLog(C_Shop.class);
  }
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance pc = client.getActiveChar();
      if (ServerRestartTimer.isRtartTime()) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("伺服器即将关机 无法使用"));
        return;
      } 
      if (!pc.isGhost() && 
        !pc.isDead() && 
        !pc.isTeleport()) {
        int mapId = pc.getMapId();
        if (mapId != 340 && mapId != 350 && mapId != 360 && mapId != 370 && mapId != 800) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(876));
          return;
        } 
        if (!pc.getMap().isSafetyZone((Point)pc.getLocation())) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(876));
          return;
        } 
        ArrayList<L1PrivateShopSellList> sellList = pc.getSellList();
        ArrayList<L1PrivateShopBuyList> buyList = pc.getBuyList();
        boolean tradable = true;
        int type = readC();
        if (type == 0) {
          int sellTotalCount = readH();
          int i = 0;
          while (i < sellTotalCount) {
            int sellObjectId = readD();
            int sellPrice = Math.max(0, readD());
            if (sellPrice <= 0) {
              _log
                .error("要求开设个人商店传回金币小于等于0: " + pc.getName() + pc.getNetConnection().kick());
              break;
            } 
            if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue()) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("无法开设商店"));
              return;
            } 
            if (sellPrice > MAX_PRICE.intValue()) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("要求开设个人商店传回数量不可大于：" + MAX_PRICE));
              break;
            } 
            int sellCount = Math.max(0, readD());
            if (sellCount <= 0) {
              _log
                .error("要求开设个人商店传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
              break;
            } 
            if (Shutdown.isSHUTDOWN) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("目前服务器准备关机状态"));
              return;
            } 
            L1ItemInstance checkItem = pc.getInventory().getItem(sellObjectId);
            if (checkItem.getItemId() == 44070 || checkItem.getItem().getnoprivateshop() > 0) {
              tradable = false;
              pc.sendPackets((ServerBasePacket)new S_ServerMessage("[" + checkItem.getItem().getName() + "]不能摆商店"));
            } 
            if (!checkItem.getItem().isTradable()) {
              tradable = false;
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1497));
            } 
            if (checkItem.get_time() != null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1497));
              tradable = false;
            } 
            if (checkItem.getBless() >= 128) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1497));
              return;
            } 
            if (checkItem.isSeal()) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(checkItem.getLogName()) + "处于绑定状态！"));
              return;
            } 
            if (checkItem.isEquipped()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(141));
              return;
            } 
            if (ItemRestrictionsTable.RESTRICTIONS
              .contains(Integer.valueOf(checkItem.getItemId()))) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(210, checkItem.getItem().getNameId()));
              return;
            } 
            if (pc.get_other().get_item_objid() != 0) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fT物品正在进行托售中,请在次重新操作一次"));
              pc.sendPackets((ServerBasePacket)new S_CloseList(pc.getId()));
              pc.get_other().set_item_objid(0);
              return;
            } 
            Object[] petlist = pc.getPetList().values().toArray();
            Object[] array;
            int k = (array = petlist).length;
            int l = 0;
            while (l < k) {
              Object petObject = array[l];
              if (petObject instanceof L1PetInstance) {
                L1PetInstance pet = (L1PetInstance)petObject;
                if (checkItem.getId() == pet.getItemObjId()) {
                  tradable = false;
                  pc.sendPackets((ServerBasePacket)new S_ServerMessage(1187));
                  return;
                } 
              } 
              l++;
            } 
            if (checkItem.getId() == pc.getQuest().get_step(74)) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, checkItem.getName(), " 无法交易"));
              return;
            } 
            if (pc.getDoll(checkItem.getId()) != null) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
              return;
            } 
            L1PrivateShopSellList pssl = new L1PrivateShopSellList();
            pssl.setItemObjectId(sellObjectId);
            pssl.setSellPrice(sellPrice);
            pssl.setSellTotalCount(sellCount);
            sellList.add(pssl);
            i++;
          } 
          int buyTotalCount = readH();
          int j = 0;
          while (j < buyTotalCount) {
            int buyObjectId = readD();
            int buyPrice = Math.max(0, readD());
            if (buyPrice <= 0) {
              _log
                .error("要求买入道具传回金币小于等于0: " + pc.getName() + pc.getNetConnection().kick());
              break;
            } 
            if (CharacterPunishInstance.get().checkCharacter(Integer.valueOf(pc.getId())).booleanValue()) {
              pc.sendPackets((ServerBasePacket)new S_SystemMessage("无法开设商店"));
              return;
            } 
            int buyCount = Math.max(0, readD());
            if (buyCount <= 0) {
              _log
                .error("要求买入道具传回数量小于等于0: " + pc.getName() + pc.getNetConnection().kick());
              break;
            } 
            L1ItemInstance checkItem2 = pc.getInventory().getItem(buyObjectId);
            if (checkItem2.getCount() > 0L) {
              if (!checkItem2.getItem().isTradable()) {
                tradable = false;
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(1497));
              } 
              if (checkItem2.get_time() != null) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(1497));
                tradable = false;
              } 
              if (checkItem2.getBless() >= 128) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(1497));
                return;
              } 
              if (checkItem2.isSeal()) {
                pc.sendPackets((ServerBasePacket)new S_SystemMessage(String.valueOf(checkItem2.getLogName()) + "处于绑定状态！"));
                return;
              } 
              if (checkItem2.isEquipped()) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(141));
                return;
              } 
              Object[] petlist2 = pc.getPetList().values().toArray();
              Object[] array2;
              int length = (array2 = petlist2).length;
              int k = 0;
              while (k < length) {
                Object petObject2 = array2[k];
                if (petObject2 instanceof L1PetInstance) {
                  L1PetInstance pet2 = (L1PetInstance)petObject2;
                  if (checkItem2.getId() == pet2.getItemObjId()) {
                    tradable = false;
                    pc.sendPackets((ServerBasePacket)new S_ServerMessage(1187));
                    return;
                  } 
                } 
                k++;
              } 
              if (checkItem2.getId() == pc.getQuest().get_step(74)) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, checkItem2.getName(), " 无法交易"));
                return;
              } 
              if (pc.getDoll(checkItem2.getId()) != null) {
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(1181));
                return;
              } 
              L1PrivateShopBuyList psbl = new L1PrivateShopBuyList();
              psbl.setItemObjectId(buyObjectId);
              psbl.setBuyPrice(buyPrice);
              psbl.setBuyTotalCount(buyCount);
              buyList.add(psbl);
            } 
            j++;
          } 
          if (!tradable) {
            sellList.clear();
            buyList.clear();
            pc.setPrivateShop(false);
            pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 3));
            return;
          } 
          byte[] chat = readByte();
          pc.setShopChat(chat);
          pc.setPrivateShop(true);
          int SelectedPolyNum = 0;
          try {
            SelectedPolyNum = 
              Integer.parseInt((new String(chat, "utf8")).split("tradezone")[1].substring(0, 1));
          } catch (Exception e) {
            e.printStackTrace();
          } 
          L1PolyMorph.doPolyPraivateShop((L1Character)pc, SelectedPolyNum);
          pc.sendPacketsAll((ServerBasePacket)new S_DoActionShop(pc.getId(), chat));
          return;
        } 
        if (type == 1) {
          sellList.clear();
          buyList.clear();
          pc.setPrivateShop(false);
          pc.sendPacketsAll((ServerBasePacket)new S_DoActionGFX(pc.getId(), 3));
          L1PolyMorph.undoPolyPrivateShop((L1Character)pc);
          return;
        } 
        return;
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
